package com.ynet.middleground.riskmanage.utils.qdg;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.ynet.middleground.riskmanage.bean.qdg.QdgAllGoodsInfoReq;

/**
 * @ClassName SignTest
 * @Description 对接青岛港工具类
 * @Author liwq
 * @Date 2023/04/03 15:26
 * @Version 1.0
 */
public class SignTest {

    public static void main(String[] args) {
        signTest();

    }

    /**
     * 生成签名
     * 
     * @return
     */
    public static String signTest() {

        try {

            // 私钥
            String privateKey =
                "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAIFWTUKa0EKY+4A3YI52Hb9X+jvsHN+NbyeuWsNr8DaDylVSd12+FtfrKzDtBlW3i23Rp836aFBejryujNaim9l/4/U4U+cdUX4wdEeut3F2ujy/ei2VKdMtWTAunHNoEI9aLf+esu7eSDKwCU41A4T6SgCEzauxoN0rB1kNkqy3AgMBAAECgYAhsIg5dDgRUOYaFNKq6SuAfvsohq1SsfWlQW5U/xW4LYYTyYJcQbqTjadXfDYtN70y4uyx46suXCMUFOeMtRihJSPeLQ6fIiRSHoEpSv6EutgPhBORv7y5U/fidwldZtknJw1Hi7TGjADQSGf8ah9ODMaGJeNSYaTFncMiFZ2iGQJBAL3yDvBISYg8avl2jJjV7zF4GKDFawrniYU856h5LUzuWlHwmAPt/WdeDXgxWMLKHWkTJrGFftVfPSqe6Lz982UCQQCuUJDVkUYVRyiOmD1XDqL08SBS6BRazcvbtY2JomuErvn1iHtPTbwU1YlMp35noEp1W5PkU1fmZDeilJ6r+NPrAkBGfUr/7sOjFFDarRz2JwZyRFBXcYwM6uoFERojUnz2PMo1+GRhqPNOZf/3p9B50Uni0aGdZZ4xDvwDEh/q6w9lAkBCY2zF6g5HDFLl/BVgkrJ9l1g4OyuFadu6UesWAEXywrFwUgqkqXZ4eNtR1njN/r5c9APfcd6IYKLPyMqltJ8BAkAgy6tEsGX9J7QZGUKMZVG06fYDXTADRlLNHaddW8yTHTEFHxXwjzl0XiVYMhgEi0EkmMTfS0JyLDYjG/xTf7i4";
            // 公钥
            // String publicKey = "publicKey";

            // data
            QdgAllGoodsInfoReq req = new QdgAllGoodsInfoReq();
            req.setTerminalCode("5");
            req.setCargoCustomerCode("91371500769712461N");
            req.setCargoCustomerName("茌平信发华宇氧化铝有限公司");
            String strdata = JSON.toJSONString(req);
            System.out.println(strdata);
            // 签名参数
            Map<String, String> parMapResult = new HashMap<String, String>();
            parMapResult.put("customerCode", "789654123658742163");
            parMapResult.put("customerName", "山东港口投资控股有限公司");
            parMapResult.put("reqTime", "2023-04-10 10:28:50");
            parMapResult.put("data", strdata);
            String parStr = SignUtil.formatUrlMap(parMapResult, false, false);
            // sign 签名
            String sign = SignUtil.sign(parStr, privateKey, "UTF-8");
            System.out.println(sign);
            return "成签名成功";

        } catch (Exception e) {
            return "验签失败";
        }
    }

    /**
     * 验签
     * 
     * @param param1
     * @param param2
     * @param sign
     * @return
     */
    public static String chenkSignTest(String param1, String param2, String sign) {

        try {

            // 私钥
            String privateKey = "privateKey";
            // 公钥
            String publicKey = "publicKey";

            // 此处开始验签操作
            Map<String, String> parMap = new HashMap<String, String>();
            parMap.put("param1", param1);
            parMap.put("param2", param2);
            // 对参数进行排序
            String parStr = SignUtil.formatUrlMap(parMap, false, false);
            // 进行验签
            boolean bo = SignUtil.doCheck(parStr, sign, publicKey);
            if (bo) {
                return "操作成功";
            } else {
                return "验签失败";
            }

        } catch (Exception e) {
            return "验签失败";
        }
    }

}
